import { message as antdMessage } from 'antd';

// 创建全局message实例
let messageApi = null;

// 设置message API实例
export const setMessageApi = (api) => {
  messageApi = api;
};

// 封装的message方法
export const message = {
  success: (content, duration = 3) => {
    if (messageApi) {
      messageApi.success({
        content,
        duration,
      });
    } else {
      antdMessage.success(content);
    }
  },
  
  error: (content, duration = 3) => {
    if (messageApi) {
      messageApi.error({
        content,
        duration,
      });
    } else {
      antdMessage.error(content);
    }
  },
  
  warning: (content, duration = 3) => {
    if (messageApi) {
      messageApi.warning({
        content,
        duration,
      });
    } else {
      antdMessage.warning(content);
    }
  },
  
  info: (content, duration = 3) => {
    if (messageApi) {
      messageApi.info({
        content,
        duration,
      });
    } else {
      antdMessage.info(content);
    }
  },
  
  loading: (content, duration = 0) => {
    if (messageApi) {
      return messageApi.loading({
        content,
        duration,
      });
    } else {
      return antdMessage.loading(content, duration);
    }
  }
};

export default message; 